package relatorios;

import java.io.InputStream;
import java.sql.Connection;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;


public class GerarRelatorio {
	public void relatorioUSALL(List lista) {
		try {
			FacesContext context = FacesContext.getCurrentInstance();
			HttpServletResponse response = (HttpServletResponse) context
					.getExternalContext().getResponse();
			ServletOutputStream responseStream = response.getOutputStream();
			// caminho do jrxml
			InputStream pathjrxml = getClass().getResourceAsStream(
					"../jrxml/reportblog.jrxml");
			response.setContentType("application/pdf");
			response.setHeader("Content-Disposition",
					"attachment; filename=\"relatoriotodosusuarios.pdf\"");
			JasperReport pathReport = JasperCompileManager
					.compileReport(pathjrxml);
			JasperPrint print = JasperFillManager.fillReport(pathReport, null,
					new JRBeanCollectionDataSource(lista));
			
//			JasperExportManager.exportReportToPdfStream(print, responseStream);
			JasperRunManager.runReportToPdfStream(pathjrxml,  
					responseStream, null,new JRBeanCollectionDataSource(lista)); 
//			//response.sendRedirect("relatoriotodosusuarios.pdf");
			
			responseStream.flush();
			responseStream.close();
			context.renderResponse();
			context.responseComplete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
